﻿using System;
using System.Data;
using System.Data.Common;
using System.Collections.Generic;
using QzEbook.Common;

/// <summary>
///CommentInfo 的摘要说明
/// </summary>
/// 
namespace QzEbook.AccessObject
{
    public class Comment : abstractAccess<Comment>
    {
        private int id;

        private int bookId;
        private int userId;
        private string userName;

        
        private string content;
        private string ip;
        private DateTime date;

        #region 属性;

        public int Id
        {
            get { return id; }
            set { id = value; }
        }
        public int BookId
        {
            get { return bookId; }
            set { bookId = value; }
        }
        public int UserId
        {
            get { return userId; }
            set { userId = value; }
        }
        public string UserName
        {
            get { return userName; }
            set { userName = value; }
        }
        public string Content
        {
            get { return content; }
            set { content= value; }
        }
        public string Ip
        {
            get { return ip; }
            set { ip = value; }
        }
        public DateTime Date
        {
            get { return date; }
            set { date = value; }
        }
        #endregion

        public Comment()
        {
        }

        /// <summary>
        /// 绑定单项;
        /// </summary>
        /// <param name="row"></param>
        /// <returns></returns>
        /// 
        protected override Comment bindRow(DataRow row)
        {
            Comment comment = new Comment();
            comment.Id = int.Parse(row["CID"].ToString());
            comment.UserId =int.Parse(row["UID"].ToString());
            comment.UserName = row["UName"].ToString();
            comment.Content = row["CComment"].ToString();
            comment.Ip = row["CIp"].ToString();
            comment.Date = DateTime.Parse(row["CDate"].ToString());
            return comment;
        }

        /// <summary>
        /// 插入一条留言;
        /// </summary>
        /// <returns></returns>
        public override bool insert()
        {
            DbCommand command = CreateCommand("InsertComment");
            addParameter(command, "@BID", BookId);
            addParameter(command, "@UID", UserId);
            addParameter(command, "@CComment", Content);
            addParameter(command, "@CIp", Ip);

            int i = -1;
            try
            {
                i = DataAccess.ExecuteNonQuery(command);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return (i != -1);
        }
        protected override DataTable selectTable()
        {
            DbCommand command = CreateCommand("SelectComment");
            addParameter(command, "@BID", BookId);
            return DataAccess.ExcuteSelectCommand(command);
        }
    }
}